<?php


namespace app\common;


use app\admin\model\Card;
use app\admin\model\Staff;
use app\admin\model\User;
use app\http\Http;
use app\sys\model\Dict;
use app\sys\model\Team;
use app\sys\model\TeamTime;
use think\Db;
use think\facade\Env;

class CommandCron
{
    public static function site()
    {
        $url = Env::get("GW.SITELIST");
        $data = Http::curlGet($url);
        $res = json_decode($data,true);
        if ($res["code"] != 200) return "gw_site code false";
        Dict::coverSiteAdd("site", $res["data"]["result"]);
        return "";
    }

    public static function userList()
    {
        $accessKeyId =  Env::get("USER_SYS.USER_SYS_ACCKEYID");
        $signature =  Env::get("USER_SYS.USER_SYS_SIGN");
        $url = Env::get("USER_SYS.USER_LIST");
        $host = Env::get("USER_SYS.USER_SYS_API");
        $s = Staff::where("delete_time", "=",0)->column("staff_no");
        foreach ($s as $k => $v){
            $data = Http::doPost($host.$url,["AccessKeyId"=>$accessKeyId,"Signature"=>$signature,"NumberKey"=>$v]);
            $res = json_decode($data,true);
            if($res["data"][0]["WorkStatus"] == "离职") {
                $deleteTime = time();
                Staff::where("staff_no", $v)->update(["delete_time"=>$deleteTime,"on_status" => -1]);
            }
        }
        return "";
    }

    // 明天上班的客服
    public static function autoUpStaffStatus()
    {
        $t = Team::column("id");
        if($t) {
            Staff::where("delete_time",0)->data(["on_status"=> 0])->update();
            Staff::where("delete_time", 0)->where("team_id","in",$t)->data(["on_status" => 1, "on_status_action_time" => 0])->update();

            $day = strtotime(date("Y-m-d"));
            $w = date('w', strtotime($day));
            if ($w == 6 || $w == 0) {

                if ($w == 0) {
                    $w = 7;
                }

                $teamIds = TeamTime::where("day", $w)->where("type", "week")->column("team_id");

                Staff::where("delete_time",0)->where("team_id", "in", $teamIds)->data(["on_status" => 0])->update();
            }

            // 休息日
            $teamIds = TeamTime::where("day", $day)->column("team_id");
            Staff::where("delete_time",0)->where("team_id", "in", $teamIds)->data(["on_status" => 0])->update();
        }
    }

    public static function cardMobile()
    {
        $arr = Card::column("user_no");
        foreach ($arr as $k => $v){
            $m = User::where("no",$v)->value("mobile","");
            card::where("user_no",$v)->data(["mobile"=>$m])->update();
        }
    }
}